package com.cqnu.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.cqnu.common.Constant;
import com.cqnu.entity.Menu;
import com.cqnu.entity.Student;
import com.cqnu.entity.SuperUser;
import com.cqnu.entity.Teacher;
import com.cqnu.entity.commonModel.AjaxResult;
import com.cqnu.entity.commonModel.Tree;
import com.cqnu.util.MD5Util;
import com.cqnu.util.WebUtil;

/**
 * 
 */
@Controller
@RequestMapping(value = "/home")
public class HomeController extends BaseController {

	/**
	 * 学生主界面
	 */
	@RequestMapping("/goStuMainPage.do")
	public String goStuMainPage(HttpServletRequest request, ModelMap modelMap) {
		
		Student sessionInfo = (Student)this.getSession(request, Constant.SESSION_USER_KEY);
		String sessionRole = (String)this.getSession(request, Constant.SESSION_ROLE_KEY);
		
		// 用户未登录--返回登录页
		if (sessionInfo == null) {
			// Message报出
			modelMap.addAttribute("errorMessage", "您还未登录或身份已过期，请登录后进行操作！");
			return "error";
		}
		// 用户角色错误--返回登录页
		if( !Constant.LOGIN_TYPE_STU.equals(sessionRole) ){
			// Message报出
			modelMap.addAttribute("errorMessage", "您没有该资源操作权限！");
			return "error";
		}
		

		List<Tree> treeList = new ArrayList<Tree>();
		
		//查询学生菜单
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("role", Constant.LOGIN_TYPE_STU);
		List<Menu> menuList = iMenuService.getMenuByRole(queryMap);
		//循环生成Tree
		for( Menu menu : menuList ){
			Tree tree = new Tree(menu);
			treeList.add(tree);
		}
		
		modelMap.addAttribute("menuList", JSON.toJSONString(treeList));
		modelMap.addAttribute("menuLeft", JSON.toJSONString(Constant.MENU_ACCORDION_STU));
		modelMap.addAttribute("sessionInfo", sessionInfo);
		return "home";
	}
	
	/**
	 * 教师主界面
	 */
	@RequestMapping("/goTeaMainPage.do")
	public String goTeaMainPage(HttpServletRequest request, ModelMap modelMap) {
		
		Teacher sessionInfo = (Teacher)this.getSession(request, Constant.SESSION_USER_KEY);
		String sessionRole = (String)this.getSession(request, Constant.SESSION_ROLE_KEY);
		
		// 用户未登录--返回登录页
		if (sessionInfo == null) {
			// Message报出
			modelMap.addAttribute("errorMessage", "您还未登录或身份已过期，请登录后进行操作！");
			return "error";
		}
		// 用户角色错误--返回登录页
		if( !Constant.LOGIN_TYPE_TEA.equals(sessionRole) ){
			// Message报出
			modelMap.addAttribute("errorMessage", "您没有该资源操作权限！");
			return "error";
		}
		

		List<Tree> treeList = new ArrayList<Tree>();
		
		//查询教师菜单
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("role", Constant.LOGIN_TYPE_TEA);
		List<Menu> menuList = iMenuService.getMenuByRole(queryMap);
		//循环生成Tree
		for( Menu menu : menuList ){
			Tree tree = new Tree(menu);
			treeList.add(tree);
		}
		
		modelMap.addAttribute("menuList", JSON.toJSONString(treeList));
		modelMap.addAttribute("menuLeft", JSON.toJSONString(Constant.MENU_ACCORDION_TEA));
		modelMap.addAttribute("sessionInfo", sessionInfo);
		return "home";
	}
	
	/**
	 * 超级用户主界面
	 */
	@RequestMapping("/goSuperMainPage.do")
	public String goSuperMainPage(HttpServletRequest request, ModelMap modelMap) {
		
		SuperUser sessionInfo = (SuperUser)this.getSession(request, Constant.SESSION_USER_KEY);
		String sessionRole = (String)this.getSession(request, Constant.SESSION_ROLE_KEY);
		
		// 用户未登录--返回登录页
		if (sessionInfo == null) {
			// Message报出
			modelMap.addAttribute("errorMessage", "您还未登录或身份已过期，请登录后进行操作！");
			return "error";
		}
		// 用户角色错误--返回登录页
		if( !Constant.LOGIN_TYPE_ADMIN.equals(sessionRole) ){
			// Message报出
			modelMap.addAttribute("errorMessage", "您没有该资源操作权限！");
			return "error";
		}
		

		List<Tree> treeList = new ArrayList<Tree>();
		
		//查询教师菜单
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("role", Constant.LOGIN_TYPE_ADMIN);
		List<Menu> menuList = iMenuService.getMenuByRole(queryMap);
		//循环生成Tree
		for( Menu menu : menuList ){
			Tree tree = new Tree(menu);
			treeList.add(tree);
		}
		
		modelMap.addAttribute("menuList", JSON.toJSONString(treeList));
		modelMap.addAttribute("menuLeft", JSON.toJSONString(Constant.MENU_ACCORDION_ADMIN));
		modelMap.addAttribute("sessionInfo", sessionInfo);
		return "home";
	}
	
	/**
	 * session权限判断，return error.jsp
	 */
	@RequestMapping("/errorInfo.do")
	public String errorInfo(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		modelMap.addAttribute("errorMessage", "您还未登录或身份已过期，请登录后进行操作！");
		return "error";
	}
	
	/**
	 * session权限判断，return error.jsp
	 */
	@RequestMapping("/errorRole.do")
	public String errorRole(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		modelMap.addAttribute("errorMessage", "您没有该资源操作权限！");
		return "error";
	}
	
	/**
	 * 异常跳转界面
	 */
	@RequestMapping("/goError.do")
	public String goError(HttpServletRequest request, HttpServletResponse response) {
		Map<String, String> maps = WebUtil.getRequestMap(request);
		logger.info("into error.htm,{} ", maps);
		return "error";
	}
	
	/**
	 * 修改密码 - 根据role判断用户
	 */
	@RequestMapping("/modifyPwd.do")
	@ResponseBody
	public AjaxResult modifyPwd(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap, 
			String loginPassword, String newPassword) throws Exception {

		Object sessionInfo = this.getSession(request, Constant.SESSION_USER_KEY);
		// 用户未登录--返回登录页
		if (sessionInfo == null) {
			response.sendRedirect(request.getContextPath()
					+ "/home/errorInfo.do");
			return null;
		}
		AjaxResult aj = new AjaxResult();
		int isSuccess = 0;
		
		String sessionRole = (String) this.getSession(request, Constant.SESSION_ROLE_KEY);
		// 判断用户角色
		if (Constant.LOGIN_TYPE_TEA.equals(sessionRole)) {
			Teacher teaInfo = (Teacher)sessionInfo;
			//判断密码是否相同
			if( teaInfo.getPassword().equals(MD5Util.MD5(loginPassword)) ){
				teaInfo.setPassword(MD5Util.MD5(newPassword));
				isSuccess = teacherService.update(teaInfo);
			} else {
				aj.setSuccess(false);
				aj.setMessage("密码错误");
				return aj;
			}
		}
		else if (Constant.LOGIN_TYPE_STU.equals(sessionRole)){
			Student stuInfo = (Student)sessionInfo;
			//判断密码是否相同
			if( stuInfo.getPassword().equals(MD5Util.MD5(loginPassword)) ){
				stuInfo.setPassword(MD5Util.MD5(newPassword));
				isSuccess = studentService.update(stuInfo);
			} else {
				aj.setSuccess(false);
				aj.setMessage("密码错误");
				return aj;
			}
		}
		else if (Constant.LOGIN_TYPE_ADMIN.equals(sessionRole)){
			SuperUser supInfo = (SuperUser)sessionInfo;
			//判断密码是否相同
			if( supInfo.getPassword().equals(loginPassword) ){
				supInfo.setPassword(newPassword);
				isSuccess = superUserService.update(supInfo);
			} else {
				aj.setSuccess(false);
				aj.setMessage("密码错误");
				return aj;
			}
		}

		if( isSuccess > 0 ){
			aj.setSuccess(true);
			aj.setMessage("修改成功!");
		} else {
			aj.setSuccess(false);
			aj.setMessage("修改失败!");
		}
		
		return aj;
	}

}
